package com.smarthome.web.property.controller.messagemanager;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.dictionary.SmsTypeService;
import com.smarthome.core.housemanager.ResidenceManagerService;
import com.smarthome.core.message.MessageTemplateService;
import com.smarthome.domain.bean.ShSmsTemplet;
import com.smarthome.domain.bean.ShSmsType;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 物业服务
 * 
 * @author hgj
 * 
 */
@Controller
@RequestMapping("/messageTemplateController")
public class MessageTemplateController {

	@Resource
	private MessageTemplateService messageTemplateService;
	@Resource
	private SmsTypeService smsTypeService;
	@Resource
	private ResidenceManagerService residenceManagerService;

	/**
	 * 获取短信模版表格
	 * 
	 * @return
	 */
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(PageHelper ph,ShSmsTemplet templet) {
		if(!UserHolder.isSuperAdmin()){
			templet.setResidenceId(UserHolder.getCurrentResidenceId());
		}
		return messageTemplateService.dataGrid(ph,templet);
	}

	/**
	 * 跳转新增页面
	 * 
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		ShSmsType type = new ShSmsType();
		type.setIsDel(0L);
		List<ShSmsType> types = smsTypeService.querySmsType(type);
		request.setAttribute("types", types);
		if(UserHolder.isSuperAdmin()){
			request.setAttribute("residence", residenceManagerService.queryShResidences(null));
		}
		request.setAttribute("isSuperadmin", UserHolder.isSuperAdmin());
		return "/messageManager/messageTemplateAdd";
	}

	/**
	 * 新增模版
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShSmsTemplet template) {
		if(null == template.getResidenceId()) template.setTypeId(UserHolder.getCurrentResidenceId());
		return messageTemplateService.saveOrUpdate(template);
	}

	/**
	 * 跳转到修改服务状态页面
	 * 
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, Long id) {
		request.setAttribute("template",
				messageTemplateService.queryTempletById(id));
		return "/messageManager/messageTemplateEdit";
	}

	/**
	 * 删除模版
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Result delete(Long id, HttpSession session) {
		int num = messageTemplateService.delete(id);
		if (num > 0) {
			return Result.success("删除成功！");
		} else {
			return Result.failure("删除失败！");
		}
	}
}
